package com.yzy.fly.boot.log;

import com.yzy.fly.FlyEnvContext;
import com.yzy.fly.config.properties.FlyProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Date;

@Order(value = 50)
@Slf4j
@Component
public class FlyAppLogCommandLineRunner implements CommandLineRunner {

    @Autowired(required = false)
    private FlyProperties flyProperties;

    @Autowired
    private FlyAppLogService flyAppLogService;

    @Override
    public void run(String... args) throws Exception {
        String startupId = FlyEnvContext.get("startupId");
        String startAppClassName = FlyEnvContext.get("StartAppClassName");
        String primarySourceClassName = FlyEnvContext.get("PrimarySourceClassName");
        boolean appLog = true;
        String appId = "";
        if (flyProperties != null) {
            appLog = flyProperties.getConfiguration().getStart().isAppLog();
            appId = flyProperties.getConfiguration().getAppId();
        }

        if (StringUtils.isBlank(appId)) {
            appId = "未设置appId";
        }
        if (appLog) {
            FlyAppLog flyAppLog = new FlyAppLog();
            flyAppLog.setAppId(appId);
            flyAppLog.setStartupId(startupId);
            flyAppLog.setStartAppClassName(startAppClassName);
            flyAppLog.setPrimarySourceClassName(primarySourceClassName);
            if (flyProperties != null) {
                String fullVersion = flyProperties.getVersion();
                if (StringUtils.isNotBlank(fullVersion)) {
                    String[] fullVersions = fullVersion.split("\\.");
                    int length = fullVersions.length;
                    String majorVersion = "";
                    String minorVersion = "";
                    String patchVersion = "";
                    String publishStatus = "";
                    if (length >= 1) {
                        majorVersion = fullVersions[0];
                    }
                    if (length >= 2) {
                        minorVersion = fullVersions[1];
                    }
                    if (length >= 3) {
                        patchVersion = fullVersions[2];
                        if (-1 != patchVersion.indexOf("-")) {
                            String[] split = patchVersion.split("-");
                            patchVersion = split[0];
                            publishStatus = split[1];
                        }
                    }
                    if (length >= 4) {
                        publishStatus = fullVersions[3];
                    }
                    flyAppLog.setMajorVersion(majorVersion);//主版本号
                    flyAppLog.setMinorVersion(minorVersion);//次版本号
                    flyAppLog.setPatchVersion(patchVersion);//修订号
                    flyAppLog.setPublishStatus(publishStatus);//发布状态

                    flyAppLog.setFullVersion(fullVersion);
                }
            }

            String pid = FlyEnvContext.get("Pid");
            String hostName = FlyEnvContext.get("HostName");
            String serverName = FlyEnvContext.get("ServerName");
            String port = FlyEnvContext.get("Port");

            flyAppLog.setPid(pid);
            flyAppLog.setHostName(hostName);
            flyAppLog.setServerName(serverName);
            flyAppLog.setPort(port);

            flyAppLog.setCreateTime(new Date());
            flyAppLog.setUpdateTime(new Date());
            flyAppLogService.insert(flyAppLog);
        }
    }

}
